package cn.edu.scau.cmi.yangjiamin.composite.consistent;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import cn.edu.scau.cmi.yangjiamin.composite.consistent.OrganizationComponent;

public class OrganizationComposite extends OrganizationComponent {

	private List<OrganizationComponent> child;// Component才有孩子

	public OrganizationComposite() {
		child = new CopyOnWriteArrayList<>();
	}

	public OrganizationComposite(String name, OrganizationComposite father) {
		super(name, father);
		child = new CopyOnWriteArrayList<>();
		// this.father = father;
	}

	public List<OrganizationComponent> getChild() {
		return child;
	}

	public boolean add(OrganizationComponent oc) {
		for (OrganizationComponent children : child) {
			if (children.getName().equals(oc.getName())) {
				return false;
			}
		}
		child.add(oc);
		return true;
	}

	public boolean remove(OrganizationComponent oc) {
		for (OrganizationComponent children : child) {
			if (children.getName().equals(oc.getName())) {
				child.remove(children);
				return true;
			}
		}
		return false;
	}

	public String display() {
		StringBuffer sb = new StringBuffer();
		for (OrganizationComponent children : child) {
			if (children instanceof OrganizationComposite) {
				sb.append("子组织: ");
				sb.append(children.getName());
				sb.append("\n");
			} else {
				sb.append("子成员: ");
				sb.append(children.getName());
				sb.append("\n");
			}
		}
		return sb.toString();
	}

}
